<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Contract;
use App\Models\ContractDetail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class ContractService
{
    public function get($search,$type){
        $sql = Contract::with('application')->with('details')->where('type',$type)->with('project')->with('user')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('title','like', '%'.$search.'%');
            });
        });
        return $sql;
    }

    public function insert($input,$details){
        $details = json_decode($details);
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = $v;
            }
        }
        try {
            $data = DB::transaction(function () use ($res,$details) {
                $data = Contract::create($res);
                foreach ($details as $key => $value) {
                    $arr_ = [];
                    $arr_['project_id'] = $res['project_id'];
                    $arr_['contract_id'] = $data->id;
                    $arr_['user_id'] = 0;
                    $arr_['name'] = $value->name;
                    $arr_['content'] = $value->content;
                    $arr_['unit_price'] = $value->unit_price;
                    $arr_['unit'] = $value->unit;
                    $arr_['work_quantity'] = $value->work_quantity;
                    $arr_['price'] = $value->price;
                    $arr_['remarks'] = $value->remarks;
                    $arr_['created_at'] = Date("Y-m-d H:i:s",time());
                    $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
                    $arr[] = $arr_;
                }
                ContractDetail::insert($arr);
                return $data;
            });
            return $data;
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input,$details){
        $details = json_decode($details);
        try {
            $data = DB::transaction(function () use ($input,$details) {
                $data = Contract::find($input['id']);
                foreach ($input as $k => $v) {
                    $data->$k = $v;
                }
                $data->save();
                foreach ($details as $key => $value) {
                    $arr_ = [];
                    $arr_['project_id'] = $input['project_id'];
                    $arr_['contract_id'] = $data->id;
                    $arr_['user_id'] = 0;
                    $arr_['name'] = $value->name;
                    $arr_['content'] = $value->content;
                    $arr_['unit_price'] = $value->unit_price;
                    $arr_['unit'] = $value->unit;
                    $arr_['work_quantity'] = $value->work_quantity;
                    $arr_['price'] = $value->price;
                    $arr_['remarks'] = $value->remarks;
                    $arr_['created_at'] = Date("Y-m-d H:i:s",time());
                    $arr_['updated_at'] = Date("Y-m-d H:i:s",time());
                    $arr[] = $arr_;
                }

                ContractDetail::where('contract_id',$data->id)->delete();
                $data = ContractDetail::insert($arr);
                return $data;
            });
            return $input['id'];
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function importInsert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v){
                $res[$k] = (array)$v;
            }
        }
        try {
            $data = ContractDetail::insert($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

}